package net.zjitc.utils;

import javax.mail.*;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;


/**
 * Created by Administrator on 2017/11/30.
 */
public class MailUtils {
    public static void sendMail(String to,String code ) throws MessagingException {
        //发送邮件
    /*
    * 1.建立一个会话
      2.创建一个代表邮件对象的message
      3.发送
    * */
        //1.建立一个sesstion会话
        Properties properties=new Properties();

        properties.setProperty("mail.host","smtp.qq.com");
        //授权
        properties.setProperty("mail.smtp.auth","true");
        properties.setProperty("mail.smtp.port","587");
        Authenticator authenticator=new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("370268540@qq.com","yabkkclbhjvxbhde");
            }
        };
        Session session=Session.getInstance(properties,authenticator);
        //创建一个代表邮件对象的Message
        Message message=new MimeMessage(session);
        message.setFrom(new InternetAddress("370268540@qq.com"));
        //发谁
        message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
        //message.addRecipient(Message.RecipientType.CC,new InternetAddress("1293515585@qq.com"));
        //设置标题
        message.setSubject("来自猫的问候");
        //设置内容（格式--html）
        String content="<a href=\"http://localhost:8086/newshop/activeCode?code="+code+"\">http://localhost:8086/newshop/activeCode?code="+code+"</a>";
        message.setContent(content,"text/html;charset=utf-8");

        //发送
        Transport.send(message);
    }

}
